package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.annotation.RateLimit;
import com.example.demo.common.BaseResponse;
import com.example.demo.common.ErrorCode;
import com.example.demo.common.ResultUtils;
import com.example.demo.exception.BusinessException;
import com.example.demo.myModel.entity.MyUser;
import com.example.demo.myModel.otd.signup.*;
import com.example.demo.service.SignUpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "竞赛报名接口")
@RestController
@RequestMapping("/signup")
@Slf4j
public class SignUpController {

    @Resource
    private SignUpService signUpService;

    @ApiOperation("提交报名")
    @PostMapping("/submit")
    @RateLimit(permitsPerSecond = 1)
    public BaseResponse<Void> submitSignUp(
            @ApiParam("报名信息") @Valid @RequestBody TeamSignUpRequest request,
            HttpServletRequest httpRequest) {
        try {
            Object userObj = httpRequest.getSession().getAttribute("loginUser");
            if (userObj == null) {
                throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
            }
            Long userId = ((MyUser) userObj).getID();
            signUpService.submitSignUp(request);
            return ResultUtils.success(null);
        } catch (BusinessException e) {
            log.error("报名失败: {}", e.getMessage());
            return ResultUtils.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("报名异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
        }
    }

    @ApiOperation("审核报名")
    @PostMapping("/audit")
    @RateLimit(permitsPerSecond = 5)
    public BaseResponse<Void> auditSignUp(
            @ApiParam("审核信息") @Valid @RequestBody SignUpAuditRequest request) {
        try {
            signUpService.auditSignUp(request);
            return ResultUtils.success(null);
        } catch (BusinessException e) {
            log.error("审核失败: {}", e.getMessage());
            return ResultUtils.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("审核异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
        }
    }

    @ApiOperation("查询报名状态")
    @GetMapping("/status")
    public BaseResponse<Page<SignUpDetailResponse>> getSignUpStatus(
            @ApiParam("查询条件") @Valid SignUpStatusRequest request,
            HttpServletRequest httpRequest) {
        try {
            Object userObj = httpRequest.getSession().getAttribute("loginUser");
            if (userObj == null) {
                throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
            }
            Long userId = ((MyUser) userObj).getID();
            Page<SignUpDetailResponse> page = signUpService.pageUserSignUps(userId, request);
            return ResultUtils.success(page);
        } catch (BusinessException e) {
            log.error("查询失败: {}", e.getMessage());
            return ResultUtils.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("查询异常", e);
            return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
        }
    }
} 